<template>
	<div class="role" v-if="dialogFormVisible">
		<el-dialog :title="title" :visible="dialogFormVisible" :close-on-click-modal="false" :fullscreen=true width="800px" top="8vh" :before-close="handleClose">
			<div class="tab">
				<div style="margin-bottom: 15px;">
					订单基础信息
				</div>
				<el-form ref="form" :rules="rules" :model="form" label-width="80px">
					<el-form-item label="产品Model" prop="project_name">
						<el-input v-model="form.project_name" :disabled="num == 3" placeholder="请输入项目名称"></el-input>
					</el-form-item>
					<el-form-item label="里程碑" prop="milepost">
						<el-select v-model="form.milepost" placeholder="请选择里程碑" style="width:100%;" :disabled="num == 3">
							<el-option
								v-for="item in milepostOptions"
								:key="item.index"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="客户属性" prop="customer_type">
						<el-select v-model="form.customer_type" placeholder="请选择状态" style="width:100%;" :disabled="num == 3">
							<el-option
								v-for="item in options"
								:key="item.index"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="客户" prop="customer">
						<el-select v-model="form.customer" placeholder="请选择客户" style="width:100%;" :disabled="num == 3">
							<el-option
								v-for="item in customerOptions"
								:key="item.index"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="产品线" prop="product_line">
						<el-select v-model="form.product_line" placeholder="请选择产品" style="width:100%;" @change="productLine" :disabled="num == 3">
							<el-option
								v-for="item in productLineOptions"
								:key="item.index"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="产品" prop="product">
						<el-select v-model="form.product" placeholder="请选择产品" style="width:100%;" :disabled="num == 3">
							<el-option
								v-for="item in productOptions"
								:key="item.index"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="测试组" prop="group">
						<el-select v-model="form.group" placeholder="请选择产品" style="width:100%;" :disabled="num == 3">
							<el-option
								v-for="item in groupOptions"
								:key="item.index"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="测试项" prop="test_item">
						<el-select
							v-model="form.test_item"
							placeholder="请选择测试项"
							style="width:100%;"
							:disabled="num == 3">
							<el-option
								v-for="(item,index) in testOption"
								:key="index"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>

				</el-form>
			</div>

			<div class="testCase" v-if="num == 3">
				<el-collapse v-model="activeNames" >
					<el-collapse-item title="测试用例" name="1">
						<div class="testTable">
							<el-table
								v-if="initTableList"
								class="marT"
								:data="initTableList"
								:border="true"
								:header-cell-style="{background:'#eef1f6'}"
								lazy
								row-key="id">
								<el-table-column align="center" type="index" label="序号" width="50">
								</el-table-column>
								<el-table-column align="center" property="case_name" label="用例标题" width="300"></el-table-column>
								<el-table-column align="center" property="priority" label="优先级"></el-table-column>
								<el-table-column align="center" property="test_type" label="测试类型"></el-table-column>
							</el-table>
						</div>
					</el-collapse-item>
				</el-collapse>
			</div>
			<div class="testTask" v-if="num == 3">
				测试任务
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
				<el-button type="primary" @click="add('form')" v-if="num != 3">提 交</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getHeader } from '@/app.config'
import {orderOptions,productList,manufactorList,productLineList,
	groupList,orderSure,orderDetail,orderCase} from '@/api/user'
export default {
	name: 'addForm',
	data(){
		return {
			dialogFormVisible:false,
			form:{
				name:''
			},
			id:'',
			rules:{
				project_name:{required: true, message: '请输入项目名称', trigger: 'blur' },
				milepost: [{ required: true, message: '请选择里程碑', trigger: 'change' }],
				customer_type:[{ required: true, message: '请选择客户类型', trigger: 'change' }],
				customer:[{ required: true, message: '请输入客户', trigger: 'blur' }],
				product_line:{ required: true, message: '请输入产品线', trigger: 'blur' },
				product:{ required: true, message: '请输入产品', trigger: 'change' },
				group:{ required: true, message: '请输入产品', trigger: 'blur' },
				test_item:[{ required: true, message: '请选择测试项', trigger: 'change' }]
			},
			options:[],
			num:0,
			title:'新增',
			optionsLeader:[],
			milepostOptions:[],
			testOption:[],
			productOptions:[],
			customerOptions:[],
			productLineOptions:[],
			groupOptions:[],
			initTableList:[],
			activeNames:1
		}
	},
	created() {

	},
	methods:{
		productLine(e){
			this.productLineOptions.filter(item => {
				if(item['name'] == e){
					let data = item['id']
					productList(data).then(res => {
						this.productOptions = res.data
					})
				}
			})
		},
		handleClose(){
			this.dialogFormVisible = false
			this.form = {}
			this.milepostOptions = []
			this.testOption = []
			this.productOptions = []
			this.customerOptions = []
			this.productLineOptions = []
			this.groupOptions = []
		},
		add(form){
			this.$refs[form].validate((valid) => {
				if (valid) {
					if(this.num == 2){
						console.log(this.id)
						var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/order/${this.id}`;
						var data = this.form
						this.$axios.post(source,data,{headers: getHeader()})
							.then((response) => {
								if (response.data.status == 'success') {
									this.dialogFormVisible = false
									this.form ={}
									this.milepostOptions = []
									this.testOption = []
									this.productOptions = []
									this.customerOptions = []
									this.productLineOptions = []
									this.groupOptions = []
									this.$parent.initTable()
									this.$message.success("修改成功!")
								}
							}).catch((err) => {
							console.log(err);
						});
					}

				} else {
					return false;
				}
			});

		},
		initData(num,data){
			this.num = num
			this.id = data
			console.log(num,data)

			orderDetail(data).then(res => {
				console.log(res,121212)
				this.form = Object.assign({},res.data)
				this.initTableList = res.data.case_list
			})

			var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/manufactor/type`;
			this.$axios.get(source,{headers: getHeader()})
				.then((response) => {
					if (response.status == 200) {
						this.options = response.data.data
					}
				}).catch((err) => {
				console.log(err);
			});

			manufactorList().then(res => {
				this.customerOptions = res.data
			})

			productLineList().then(res => {
				this.productLineOptions = res.data

			})

			productList().then(res => {
				this.productOptions = res.data
			})

			groupList().then(res => {
				this.groupOptions = res.data
			})

			orderOptions().then(res => {
				var data = res.data
				data.mileposts.filter(item => {
					this.milepostOptions.push({name:item})
				})
				data.test_items.filter(item => {
					this.testOption.push({name:item})
				})
			})



			if(num == 2){
				this.title="编辑"
				this.form = Object.assign({},data)
			}
			if(num == 3){
				this.title="查看"
				// this.form = Object.assign({},data)

			}
		}
	}

}
</script>

<style lang="scss" scoped>
.role{

}
.testTable{
	height: 430px;
	overflow:scroll;
}
</style>
